
var AddClass=new Array(); //存放所有用户的数组
var operateType; // 按钮的操作类型
function ClassInfo(classId,className,gradeName,headTeachName,classSlogan,classPeople) {
    this.classId=classId;
    this.className=className;
    this.gradeName=gradeName;
    this.headTeachName=headTeachName;
    this.classSlogan=classSlogan;
    this.classPeople=classPeople;
}

function show2() {
    AddClass[0]=new ClassInfo("1","1班","高一（1）班","封瑞","一起拼搏","23");
    AddClass[1]=new ClassInfo("2","2班","高一（2）班","孙梦月","好好学习","32");
    AddClass[2]=new ClassInfo("3","3班","高一（3）班","李佳肴","考上大学","30");
    AddClass[3]=new ClassInfo("4","4班","高一（4）班","郑笨蛋","找个好工作","25");
    refresh(AddClass);
}
// 刷新表格
function refresh(AddClass) {
    var tbody = document.getElementById("tbody");
    var html = "";
    var color = "warning";
    var flag = true;
    for (var i in AddClass) {
        if (flag) {
            color = "info";
        } else {
            color = "warning";
        }
        html = html + "<tr class='" + color + "'><td style='width:30px;'><input type='checkbox'></td><td id='code'>"
            /*+ AddClass[i].code + "</td><td id='classId'>"*/
            + AddClass[i].classId + "</td><td id='className'>"
            + AddClass[i].className + "</td><td id='gradeName'>"
            + AddClass[i].gradeName + "</td><td id='headTeachName'>"
            + AddClass[i].headTeachName + "</td><td id='classSlogan'>"
            + AddClass[i].classSlogan + "</td><td id='classPeople'>"
            + AddClass[i].classPeople + "</td>"
            + "</tr>";
        flag = !flag;//颜色转换
    }
    tbody.innerHTML = html; // 在表格中创建所有tr元素
}
// 当点击添加按钮
$("#class_add").on("click", function () {
    operateType = "add";
});
//当点击修改按钮
$("#class_edit").on("click", function () {
    operateType = "edit";
});


//添加用户
$("#save").on("click", function () {
    if (operateType == "add") {
        var newClass = new ClassInfo($("#m_code").val(), $("#m_className").val(), $("#m_gradeName").val(), $("#m_headTeachName").val(), $("#m_classSlogan").val(), $("#m_classPeople").val());
        /*users = users.concat(newUser);*/
        AddClass = AddClass.concat(newClass);

    } else if (operateType == "edit") {
        for(var i = 0; i< AddClass.length;i++){
          /*  if(code == AddClass[i].code)*/
            if(classId == AddClass[i].classId)
            {
                AddClass[i].classId =  $("#m_code").val();
                AddClass[i].className = $("#m_className").val();
                AddClass[i].gradeName =  $("#m_gradeName").val();
                AddClass[i].headTeachName = $("#m_headTeachName").val();
                AddClass[i].classSlogan = $("#m_classSlogan").val();
                AddClass[i].classPeople = $("#m_classPeople").val();
            }
        }
        alert("修改成功333");
    }

    refresh(AddClass);
});

var newAddClass;
// 查询用户
$("#class_find").on("click", function () {
    newAddClass = new Array();
    var codeLike = $(this).siblings("#ss_code").val();
    for(var i = 0; i< AddClass.length;i++){
        if(AddClass[i].classId.indexOf(codeLike)>=0) {
            newAddClass = newAddClass.concat( AddClass[i] );
        }
    }
    refresh(newAddClass);
});

//删除用户
$("#class_delete").on("click", function () {
    var checkboxs=$("input[type='checkbox']");
    let f=false;
    for (var i = 0; i < checkboxs.length; i++) {
        var box = checkboxs[i];
        if (box.checked) {
            f= true;
            box=$(box);
           /* let code=box.parent().siblings("#code").text();*/
            let classId=box.parent().siblings("#code").text();
            for(var i=0; i<AddClass.length;i++){
                if(classId == AddClass[i].classId){
                    AddClass.splice(i,1);
                    alert("删除成功33");
                }
            }
        }
    }
    if(!f){
        alert("选择删除的用户");
    }
    refresh(AddClass);
});

var code;


//编辑用户
$("#class_edit").on("click", function () {
    var checkboxs=$("input[type='checkbox']");
    let f=false;
    for (var i=0; i<checkboxs.length;i++) {
        var box=checkboxs[i];
        if (box.checked) {
            f=true;
            $(this).attr("data-toggle","modal");  // 启用模态框
            $(this).attr("data-target","#myModal"); // 启用模态框
            box=$(box);
              let classId=box.parent().siblings("#code").text();
            /*code=box.parent().siblings("#code").text();*/
            // 获取表格中选中的用户的各项数据
            var className=box.parent().siblings("#className").text();
            var gradeName=box.parent().siblings("#gradeName").text();
            var headTeachName=box.parent().siblings("#headTeachName").text();
            var classSlogan=box.parent().siblings("#classSlogan").text();
            var classPeople=box.parent().siblings("#classPeople").text();
            // 选中用户的各项数据，放在模态框的相应input框中
           /* $("#m_code").val(code);*/
            $("#m_code").val(classId);
            $("#m_className").val(className);
            $("#m_gradeName").val(gradeName);
            $("#m_headTeachName").val(headTeachName);
            $("#m_classSlogan").val(classSlogan);
            $("#m_classPeople").val(classPeople);
            alert("走最里面没");
        }
        alert("走外面没");
    }
    if(!f){
        alert("选择编辑的用户");
        $(this).attr("data-toggle",null); // 禁用模态框
        $(this).attr("data-target",null); // 禁用模态框
    }
});